package com.my.thymeleaf.Controller;

import com.my.thymeleaf.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @GetMapping("/getStudent")
    public String TestStudentController(HttpServletRequest request){
        Student student = new Student();
        student.setId(1001);
        student.setName("张梦杰");
        student.setAge(19);
        request.setAttribute("TestThymeleaf",student);
        return "Hello";
    }
    @GetMapping("/get")
    @ResponseBody
    public String test(){
        return "url测试成功";
    }

    /**
     *List 循环遍历
     */
    @GetMapping("/xunhuan")
    public String test_1(HttpServletRequest request) {
        ArrayList<Student> arrayList = new ArrayList<>();
        Student student = new Student();
        student.setId(1001);
        student.setName("zmj");
        student.setAge(23);
        Student student1 = new Student();
        student1.setId(1002);
        student1.setName("xlj");
        student1.setAge(22);
        arrayList.add(student);
        arrayList.add(student1);
        request.setAttribute("arrayList",arrayList);
        return "ListEach";
    }
    /**
     * Map集合循环遍历
     */
    @GetMapping("/mapEach")
    public String mapEach(HttpServletRequest request){
        Map<String,Student> hashMap = new HashMap<>();
        hashMap.put("stu1",new Student(1001,"夏天",19));
        hashMap.put("stu2",new Student(1002,"张三",21));
        hashMap.put("stu3",new Student(1003,"李四",22));
        request.setAttribute("HashMap",hashMap);
        return "MapEach";

    }
    /**
     * List_Map集合遍历
     */
    @GetMapping("List_Map")
    public String List_Map(HttpServletRequest request){
        Map<String,Student> hashMap1 = new HashMap<>();
        hashMap1.put("stu1",new Student(1001,"夏1",19));
        hashMap1.put("stu2",new Student(1002,"夏2",21));
        hashMap1.put("stu3",new Student(1003,"夏3",22));
        request.setAttribute("HashMap",hashMap1);

        List<Map<String,Student>> list_Map = new ArrayList<>();
        list_Map.add(hashMap1);
        Map<String,Student> hashMap = new HashMap<>();
        hashMap.put("stu1",new Student(1001,"夏天",19));
        hashMap.put("stu2",new Student(1002,"张三",21));
        hashMap.put("stu3",new Student(1003,"李四",22));
        list_Map.add(hashMap);
        request.setAttribute("ListMap",list_Map);
        return "List_Map";
    }
}
